package com.lanou3g.code0516;

import com.lanou3g.code0516.controller.Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "registerServlet", urlPatterns = "/register")
public class registerServlet extends HttpServlet {
    private Controller controller = new Controller();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        // 拿到用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String word = request.getParameter("word");
        // 验证
        boolean isSuccess = controller.register(username, password, word);
        // 根据验证结果构建验证信息
        String msg = "";
        if (isSuccess) {
            msg = "注册成功";
        } else {
            msg = "注册失败";
        }
        //把消息放到request域中发送给jsp
        request.setAttribute("msg", msg);
        //请求转发
        request.getRequestDispatcher("/user/msg.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
